package com.wx.mapper;

import com.wx.dto.CarQueryDTO;
import com.wx.dto.CollectionOrOrderDTO;
import com.wx.dto.ConditionGetCarDTO;
import com.wx.entity.Car;
import com.wx.entity.Favor;
import com.wx.entity.Order;
import com.wx.entity.User;
import com.wx.vo.FavorVO;
import com.wx.vo.OrderQueryVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CarMapper {

    /*关键词搜索汽车*/
    List<Car> carList(CarQueryDTO carQueryDTO);

    /*根据汽车编号和用户名查询收藏记录*/
    @Select("select * from favor where username=#{username} and car_no = #{carNo}")
    Favor queryCollectionByCarNo(CollectionOrOrderDTO collectionOrOrderDTO);

    /*新增收藏*/
    @Insert("insert into favor(username,car_no)" +
            "values(#{username},#{carNo})")
    void addCollection(CollectionOrOrderDTO collectionOrOrderDTO);

    /*获取用户收藏*/
    List<FavorVO> getcollection(CollectionOrOrderDTO collectionOrOrderDTO);

    /*用户取消收藏*/
    @Delete("delete from favor where id = #{id}")
    void infavor(Integer id);

    //根据carNo查询汽车数量
    @Select("select amount from car where car_no = #{carNo};")
    Long queryCarByCarNo(String carNo);

    //汽车数量自减
    @Update("update car set amount = amount - 1 where car_no = #{carNo}")
    void decCarAmount(String carNo);

    //添加用户订单
    /*order表名与关键字冲突*/
    @Insert("insert into `order`(username,car_no,is_finish,add_date)" +
            "values(#{username},#{carNo},#{isFinish},#{addDate})")
    void addOrder(Order order);

    //获取用户订单
    List<OrderQueryVO> getOrder(User user);

    //用户取消订单
    @Delete("delete from `order` where id=#{id}")
    void delorder(Integer id);

    //根据条件查询汽车
    List<Car> carCondition(ConditionGetCarDTO conditionGetCarDTO);
}
